<?php
/**
 * Created by PhpStorm.
 * User: sunbo
 * Date: 2019/7/10
 * Time: 5:25 PM
 */

namespace app\index\controller;
use think\Exception;


/**
 *
 * 用户升级规则
 * 普通用户-》下一级人员少于10人
 * 大户-》下一级人员超过10人,并且下一级所有人的代币超过一定数量
 */
class Friend extends CoinBaseContorller
{
    //根据用户获取 用户下一级的代币数量
    public function  GetUserNestOneCustomCoinBalance($guid){
        $row= $this->db_select("select IFNULL(sum(b.token_count),0) token_count from token_sys_user a INNER JOIN token_user_custom_coin_balance b on b.token_sys_user_guid=a.guid WHERE a.parent_guid = '".$guid."'");
        return $row[0]["token_count"];
    }

    //根据用户获取 用户下级所有的代币数量
    public function  GetUserALLCustomCoinBalance($guid){
        $row= $this->db_select("select IFNULL(sum(b.token_count),0) token_count from token_sys_user a INNER JOIN token_user_custom_coin_balance b on b.token_sys_user_guid=a.guid WHERE a.level like '%".$guid."%'");
        return $row[0]["token_count"];
    }

    //普通用户根据推荐人数 开启10开层条件
    public  function  OpenLevel($guid){
        $row =$this->db_select("select count(*) counts from token_sys_user where guid='".$guid."' and isopen_level=0");
        //已经开启就不处理
        if($row[0]["counts"]==0){
            return;
        }
        $row =$this->db_select("SELECT value from token_sys_config where guid='2'");
        $count=$row[0]["value"];
        $row =$this->db_select("select count(*) counts from token_sys_user where parent_guid='".$guid."'");
        if($row[0]["counts"]>=$count-1){
           $this->db_exec(array("update token_sys_user set isopen_level=1 where guid='".$guid."'"));
        }

    }

    //全体用户升级
    public  function UpLevel(){
         //升级到大户
        $this->UpOne();
        //升级到大咖
        $this->UpTwo();
        //升级到大神
        $this->UpThree();
        //升级到创世
        $this->UpFour();
    }

    //升级大户  升级条件 推荐 10个人  并且下级 人员代币总值超过 系统默认值
    public function  UpOne(){
        $row=$this->db_select("select value from token_sys_config where guid='12'");
        //普通用户升级大户条件之一 推荐人数超过10人
        $recommended_count=$row[0]["value"];

        $row=$this->db_select("select value from token_sys_config where guid='3'");
        //系统默认值的代币数量 必须超过
        $sumcont=$row[0]["value"]*pow(10,8);


        $userrow=$this->db_select("select guid from token_sys_user a inner join (select parent_guid from token_sys_user  GROUP BY parent_guid HAVING COUNT(parent_guid)>=".$recommended_count.") b on a.guid=b.parent_guid where a.token_sys_role_guid='1'");
        $array=array();
        for($x=0;$x<count($userrow);$x++){
            $userid=$userrow[$x]["guid"];
            //获取下级 所有代币数量
            $tokencount=$this->GetUserNestOneCustomCoinBalance($userid);
            if($tokencount>=$sumcont){
                $array[]="update token_sys_user set token_sys_role_guid='2' where guid='".$userid."'";
            }
        }
        $this->db_exec($array);
    }

    //$level 当前角色级别 $nextlevel 下一角色级别
    function _upotherLevel($level,$nextlevel){
        $row=$this->db_select("select value from token_sys_config where guid='4'");
        //获取升级条件 就是下一级 的 必须 比上一级多3个人
        $cont=$row[0]["value"];
        $userrow=$this->db_select("select guid from token_sys_user where token_sys_role_guid='".$level."'");
        $array=array();
        for($x=0;$x<count($userrow);$x++){
            $userid=$userrow[$x]["guid"];
            $row1=$this->db_select("select count(*) counts from token_sys_user where token_sys_role_guid='".$level."' and parent_guid='".$userid."'");
            if($cont<=$row1[0]["counts"]){
                $array[]="update token_sys_user set token_sys_role_guid='".$nextlevel."' where guid='".$userid."'";
            }
        }
        $this->db_exec($array);
    }

    //升级大咖  有3个大户
    public function  UpTwo(){
        $this->_upotherLevel("2","3");
    }

    //升级大神  有3个大咖
    public function  UpThree(){
        $this->_upotherLevel("3","4");
    }

    //升级创世  有3个大神
    public function  UpFour(){
        $this->_upotherLevel("4","5");
    }

    //计算收益
    public  function ExecEarning(){
        $array=array();
        //用户月收益
        $array=$this->MonthEarning($array);
        //普通用户1层收益
        $array=$this->NormalEarning($array);
        //普通用户10层收益
        $array=$this->Normal10Earning($array);
        //大户收益
        $array=$this->DHEarning($array);
        //大咖收益
        $array=$this->DGEarning($array);
        //大神收益
        $array=$this->DSEarning($array);
        //创世收益
        $array=$this->CSEarning($array);
        $array=$this->UpdateBalace($array);
        return $this->db_exec($array);
    }
    //用户月收益
    public function MonthEarning(&$array){
       //获取月收益利率
        $row= $this->db_select("select ratio from token_sys_earnings_detail where guid='2'");
        $ratio=$row[0]["ratio"];
        $array[]="insert into token_bill_earnings (guid,token_sys_user_guid,billdate,sum_tokencount,earnings_tokencount,type,token_sys_earnings_detail_guid)
select UUID(),a.token_sys_user_guid,now(),a.token_count,a.token_count*".$ratio." earning_count,0,'2' from token_user_custom_coin_balance a where  a.startdate='".date("Y-m-d")."'";
        return $array;

    }

    //普通用户1层收益
    public function NormalEarning(&$array){
        //获取1层利率
        $row= $this->db_select("select ratio from token_sys_earnings_detail where guid='3'");
        $ratio=$row[0]["ratio"];
        //获取当天的用户
        $row= $this->db_select("select a.token_sys_user_guid from token_user_custom_coin_balance a INNER JOIN token_sys_user b on a.token_sys_user_guid=b.guid where b.token_sys_role_guid='1' and b.isopen_level=0 and a.startdate='".date("Y-m-d")."'");
        for($x=0;$x<count($row);$x++){
            $userid=$row[$x]["token_sys_user_guid"];
            $array[]="insert into token_bill_earnings (guid,token_sys_user_guid,billdate,sum_tokencount,earnings_tokencount,type,token_sys_earnings_detail_guid,friend_token_sys_user_guid)
select UUID(),'".$userid."',now(), a.token_count ,a.token_count*".$ratio." earning_count,1,3,b.guid from token_user_custom_coin_balance a INNER JOIN token_sys_user b on a.token_sys_user_guid= b.guid where b.parent_guid='".$userid."'";
        }
        return $array;
    }


    //普通用户10层收益
    public function Normal10Earning(&$array){
        //获取层数
        $row=$this->db_select("select value from token_sys_config where guid='2'");
        $levelcount=$row[0]["value"];
        //获取10层利率
        $row= $this->db_select("select ratio from token_sys_earnings_detail where guid='4'");
        $ratio=$row[0]["ratio"];
        //获取当天的用户
        $row= $this->db_select("select a.token_sys_user_guid,b.levelcount from token_user_custom_coin_balance a INNER JOIN token_sys_user b on a.token_sys_user_guid=b.guid where b.token_sys_role_guid='1' and b.isopen_level=1 and a.startdate='".date("Y-m-d")."'");
        for($x=0;$x<count($row);$x++){
            $userid=$row[$x]["token_sys_user_guid"];
            $level=$row[$x]["levelcount"];
            $array[]="insert into token_bill_earnings (guid,token_sys_user_guid,billdate,sum_tokencount,earnings_tokencount,type,token_sys_earnings_detail_guid,friend_token_sys_user_guid)
select UUID(),'".$userid."',now(), a.token_count,a.token_count*".$ratio." earning_count,1,4,b.guid from token_user_custom_coin_balance a INNER JOIN token_sys_user b on a.token_sys_user_guid= b.guid where b.levelcount <=".($level+$levelcount)." and b.level like '%".$userid."%'";
        }
        return $array;
    }


    //大户收益
    public function DHEarning(&$array){
        $array=$this->Earning($array,"2","5");
        return $array;
    }

    //大咖收益
    public function DGEarning(&$array){
        $array=$this->Earning($array,"3","6");
        return $array;
    }

    //大神收益
    public function DSEarning(&$array){
        $array=$this->Earning($array,"4","7");
        return $array;
    }

    //创世收益
    public function CSEarning(&$array){
        $array=$this->Earning($array,"5","8");
        return $array;
    }

    public function Earning(&$array,$role,$earnings_detail_guid){
        //获取 下级节点等级超过本级节点时 的利率
        $row=$this->db_select("select ratio  from token_sys_earnings_detail where guid='9'");
        $t_ratio=$row[0]["ratio"];

        //获取利率
        $row=$this->db_select("select ratio from token_sys_earnings_detail where guid='".$earnings_detail_guid."'");
        $ratio=$row[0]["ratio"];

        //获取当天的用户
        $row= $this->db_select("select a.token_sys_user_guid,c.level from token_user_custom_coin_balance a INNER JOIN token_sys_user b on a.token_sys_user_guid=b.guid inner join token_sys_role c on c.guid=b.token_sys_role_guid where b.token_sys_role_guid='".$role."' and  a.startdate='".date("Y-m-d")."'");
        for($x=0;$x<count($row);$x++){
            $userid=$row[$x]["token_sys_user_guid"];
            $role_level=$row[$x]["level"];
            $array[]="insert into token_bill_earnings (guid,token_sys_user_guid,billdate,sum_tokencount,earnings_tokencount,type,token_sys_earnings_detail_guid,friend_token_sys_user_guid)
select UUID(),'".$userid."',now(),a.token_count ,a.token_count*".$ratio." earning_count,1,'".$earnings_detail_guid."',b.guid from token_user_custom_coin_balance a
INNER JOIN token_sys_user b on a.token_sys_user_guid= b.guid INNER JOIN token_sys_role c on c.guid=b.token_sys_role_guid WHERE b.level like '%".$userid."%' and c.level<=".$role_level."
UNION ALL
select UUID(),'".$userid."',now(), a.token_count,a.token_count*".$t_ratio." earning_count,1,'9',b.guid  from token_user_custom_coin_balance a
INNER JOIN token_sys_user b on a.token_sys_user_guid= b.guid INNER JOIN token_sys_role c on c.guid=b.token_sys_role_guid WHERE b.level like '%".$userid."%' and c.level>".$role_level;
        }
        return $array;
    }

    public function UpdateBalace(&$array){
        $array[]="update token_user_custom_coin_balance a INNER JOIN 
(select SUM(earnings_tokencount) earnings_tokencount,token_sys_user_guid,billdate from token_bill_earnings where billdate='".date("Y-m-d")."' GROUP BY token_sys_user_guid) b 
 on a.startdate=b.billdate and a.token_sys_user_guid=b.token_sys_user_guid set a.token_count=a.token_count+b.earnings_tokencount";

        $row=$this->db_select("select value from token_sys_config where guid='1'");
        $date_count=$row[0]["value"];
        $array[]="update token_user_custom_coin_balance set startdate=enddate,enddate=DATE_ADD(enddate,INTERVAL ".$date_count." DAY) WHERE startdate='".date("Y-m-d")."'";
        return $array;
    }
}